<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="common/include :: header('结算单列表')" />
</head>
<body>
<div class="ui-layout-center">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">

                    <div class="col-sm-12" >
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">客户姓名：</label>
                                <label class="col-sm-6 control-label">[[${statement.customerName}]]</label>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">联系电话：</label>
                                <label class="col-sm-6 control-label">[[${statement.customerPhone}]]</label>
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-12" >
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">车牌号码：</label>
                                <label class="col-sm-6 control-label">[[${statement.licensePlate}]]</label>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">汽车类型：</label>
                                <label class="col-sm-6 control-label">[[${statement.carSeries}]]</label>
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-12" >
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">服务类型：</label>
                                <label class="col-sm-6 control-label">[[${@dict.getLabel('si_service_catalog',statement.serviceType)}]]</label>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">到店时间：</label>
                                <label class="col-sm-6 control-label" th:text="${#dates.format(statement.actualArrivalTime, 'yyyy-MM-dd HH:mm')}"></label>
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-12" >
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">总消费金额：</label>
                                <label class="col-sm-6 control-label" id="totalAmount">[[${statement.totalAmount}]]</label>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">实付价格：</label>
                                <label class="col-sm-6 control-label" id="actuallyPaid">[[${statement.totalAmount-statement.discountAmount}]]</label>
                            </div>
                        </div>
                    </div>

                    <div class="col-sm-12" >
                        <div class="col-sm-6">
                            <div class="form-group">
                                <label class="col-sm-6 control-label">优惠价格：</label>
                                <input class="col-sm-6" id="discountAmount" type="number" onchange="discountFun()"
                                       name="discountAmount" th:field="${statement.discountAmount}"/>
                            </div>
                        </div>
                    </div>
            </div>

            <div class="btn-group-sm" id="MyTools" role="group">
                <a class="btn btn-success" onclick="saveItem()" shiro:hasPermission="system:post:add">
                    <i class="fa fa-plus"></i> 保存
                </a>
                <a class="btn btn-warning" onclick="payFun()" shiro:hasPermission="system:post:add">
                    <i class="fa fa-plus"></i> 确认支付
                </a>
            </div>

            <!--注册左边的表格-->
            <div class="col-sm-12 select-table table-striped">
                <table id="statementItemTable">
                </table>
            </div>
        </div>
    </div>
</div>

<!--右边的框-->
<div class="ui-layout-east">
    <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="post-form">
                    <div class="select-list">
                        <ul>
                            <li>
                                名称：<input type="text" name="name"/>
                            </li>
                            <li>
                                是否套餐：
                                <select name="carPackage" th:with="type=${@dict.getType('si_car_package')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                                </select>
                            </li>
                            <li>
                                服务分类：
                                <select name="serviceCatalog" th:with="type=${@dict.getType('si_service_catalog')}">
                                    <option value="">所有</option>
                                    <option th:each="dict : ${type}" th:text="${dict.label}" th:value="${dict.value}"></option>
                                </select>
                            </li>
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">

            </div>

            <!--注册表格(右边的表格)-->
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-table">
                </table>
            </div>
        </div>
    </div>
</div>
</body>

<th:block th:include="common/include :: footer" /> <!--引入分页-->
<th:block th:include="common/include :: layout-latest" />
<script>
    // OUTER-LAYOUT
    $('body').layout({
        east__size:	'30%',
    });

    $(function() {
        var statementId = [[${statement.id}]];
        console.log(statementId);
        var options = {
            id: "statementItemTable", //左边表格
            toolbar: "MyTools", //左边的工具栏
            uniqueId: "itemId", //左边表的id
            url: "/business/statementItem/query?statementId="+statementId,
            columns: [
                {
                    field: 'id',
                    title: '客户id',
                    visible: false   //隐藏
                },
                {
                    field: 'itemId',
                    title: '单项id',
                    visible: false
                },
                {
                    field: 'itemName',
                    title: '服务项名称',
                    sortable: true
                },
                {
                    field: 'itemPrice',
                    title: '服务项原价',
                    sortable: true
                },

                {
                    field: 'itemQuantity',
                    title: '购买数量',
                    sortable: true
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" ' +
                            ' onclick="addItem(\'' + row.itemId + '\')"><i class="fa fa-plus"></i></a>  ');  //加
                        actions.push('<a class="btn btn-danger btn-xs " href="javascript:void(0)" ' +
                            ' onclick="decrItem(\'' + row.itemId + '\')"><i class="fa fa-minus"></i></a>  '); //减
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //右边表格
    $(function() {
        var options = {
            url: "/business/serviceItem/query?saleStatus=1", //查询养修服务表中的已经上架的产品
            uniqueId: "id",
            columns: [
                {
                    field: 'id',
                    title: '客户id',
                    visible: false  //隐藏
                },
                {
                    field: 'name',
                    title: '服务项名称',
                    sortable: true
                },
                {
                    field: 'originalPrice',
                    title: '服务项原价',
                    sortable: true
                },

                {
                    field: 'info',
                    title: '备注信息',
                    sortable: true,
                    formatter: function(value, row, index) {
                        return $.table.tooltip(value, 0, "open");
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        actions.push('<a class="btn btn-info btn-xs " href="javascript:void(0)" ' +
                            ' onclick="addItem(\'' + row.id + '\')"><i class="fa fa-plus"></i></a>  ');
                        return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });


    // + 数据
    function addItem(id){
        var rs=$('#bootstrap-table').bootstrapTable('getRowByUniqueId',id); //拿到点击 + 这条数据
        // 拿到左边这些列的值
        var newData = {"itemName":rs.name,"itemId":id ,"itemPrice":rs.originalPrice,"itemQuantity":1};

        // 把右侧选中的数据追加左侧表格中
        var statementData = $('#statementItemTable').bootstrapTable('getRowByUniqueId',id);
        // 判断左侧中数据是否存在 , 如果存在数据量+1,如果不存在就追加一个
        if(statementData){
            // 加1 ( updateCellByUniqueId 修改)
            $('#statementItemTable').bootstrapTable('updateCellByUniqueId', {
                id: id,
                field: 'itemQuantity',
                value: statementData.itemQuantity + 1
            })
        }else{
            //追加一个 (append 追加)
            $('#statementItemTable').bootstrapTable('append', newData);
        }
        discountFun()
    }

    // -数据
    function decrItem(id){
        // 拿到 - 这条数据
        var statementData = $('#statementItemTable').bootstrapTable('getRowByUniqueId',id);
        // 拿到左边的购买数量做判断,要是大于1购买数量就减少1, 否者等于1的话就直接删除
        if(statementData.itemQuantity > 1){
            // 让购买数量减少1
            $('#statementItemTable').bootstrapTable('updateCellByUniqueId', {
                id: id,
                field: 'itemQuantity',
                value: statementData.itemQuantity - 1
            })
        }else{
            $('#statementItemTable').bootstrapTable('removeByUniqueId',id); // 等于1的话就直接删除
        }
        discountFun()
    }

    // 计算左边上面的金额算法,判断
    function discountFun(){
        // 1.获取左侧表格中的所有数据
        var data= $('#statementItemTable').bootstrapTable('getData',{useCurrentPage:false,includeHiddenRows:false});
        var totalAmount = 0; //定义总金额初始化变量为0
        // 2.循环遍历出 计算出总金额(数量*价格)
        $.each(data,function (index,item) {
            totalAmount += item.itemPrice * item.itemQuantity;
        })

        // 3.获取优惠金额
        var discountAmount = $("#discountAmount").val();
        // 判断如果 优惠金额 没有值设置为:0
        if(!discountAmount){
            $("#discountAmount").val(0)
            return;
        }
        // 判断如果优惠金额小于0 就设置成:0
        if(discountAmount<0){
            $("#discountAmount").val(0)
            return;
        }

        // 判断表格中是否有数据 ,如果没有数据把,优惠金额,实付金额,设置成0
        if(data.length == 0){
            $("#totalAmount").text(0);   //总金额
            $("#discountAmount").val(0); //优惠金额
            $("#actuallyPaid").text(0)   //实付金额
            return;
        }

        // 判断优惠金额不能大于总金额,否则设置:0
        if(discountAmount>totalAmount){
            $.modal.alert("优惠后的金额不能够大于总金额");
            $("#discountAmount").val(0)
            $("#actuallyPaid").text(totalAmount);
            return;

        }

        // 计算出实付金额
        var actualPrice = totalAmount - discountAmount;
        // 把计算出来内容 设置label中
        $("#totalAmount").text(totalAmount);
        $("#actuallyPaid").text(actualPrice);

    }


    // 保存操作
    function saveItem() {
        var statementId = [[${statement.id}]];
        // 拿到左侧表格中数据
        var data= $('#statementItemTable').bootstrapTable('getData',{useCurrentPage:false,includeHiddenRows:false});
        $.each(data,function (index,item) {
            item.statementId = statementId;
        })

        //往data里添加 优惠金额,和结算单的id
        data.push({"itemPrice":$("#discountAmount").val(),"statementId":statementId});

        var config = {
            url: ctx + "/business/statementItem/saveItems",
            type: 'post',
            dataType: 'json', //数据类型 :json
            contentType:"application/json",
            data: JSON.stringify(data), //把数据装换为json格式
            beforeSend: function () {
                $.modal.loading("正在处理中，请稍后...");
            },
            success: function(result) {

                $.operate.ajaxSuccess(result);
            }
        };
        $.ajax(config)

    }


    // 确认支付
    function payFun() {
        var statementId = [[${statement.id}]];
        $.modal.confirm("确认支付吗", function() {
            $.operate.get("/business/statementItem/pay?statementId="+ statementId);

            })

    }


</script>
</html>